#!/bin/bash
# redis安装脚本
source ./redis_config.sh

### server redis日志
SERVER_REDIS_LOG=$SERVER_LOG'/redis'
if [ ! -d ${SERVER_REDIS_LOG} ];then
echo_fun 2 服务器没有redis日志目录，创建目录${SERVER_REDIS_LOG}
mkdir -p ${SERVER_REDIS_LOG}
fi
### server redis日志



# 部署
echo_fun 1 $PLAT"上redis部署安装"
SUCCESS=true
if [ $PLAT == "MACOSX" ];then
# start MACOSX
    # redis安装的位置
    REDIS_INSTALL_PATH='/usr/local/Cellar/redis'

    # 判断是否安装了redis
    if [ ! -d ${REDIS_INSTALL_PATH} ];then
        echo_fun 2 '使用brew安装redis到目录'${REDIS_INSTALL_PATH}
        brew install redis
        if [ $? -ne 0 ];then
            SUCCESS=false
        fi # [ $? -ne 0 ];
    else
        echo_fun 2 '已安装redis，跳过安装步骤'
        SUCCESS=true
    fi
# end MACOSX
else
# start LINUX
    echo_fun 2 ${PLAT}安装脚本没有实现
    exit 0
    # yum install cpp binutils glibc-kernheaders glibc-common glibc-devel gcc make wget    #安装依赖库
    # wget http://download.redis.io/releases/redis-4.0.1.tar.gz
    # if [ -f /root/redis-4.0.1.tar.gz ];then
    #     tar zxvf redis-4.0.1.tar.gz
    #     mv redis-4.0.1 /usr/local/redis
    #     cd /usr/local/redis
    #     make
    #     cd src/
    #     make install 
    # else
    #     echo "文件不存在!"
    #     exit;
    # fi

    # sed -i '136s/daemonize no/daemonize yes/' /usr/local/redis/redis.conf    #(编辑redis服务配置文件,修改其中配置)
    # sed -i '69s/127.0.0.1/0.0.0.0/' /usr/local/redis/redis.conf 
    # sed -i '88s/protected-mode yes/protected-mode no/' /usr/local/redis/redis.conf

    # mkdir -p /etc/redis
    # ln -s /usr/local/redis/redis.conf /etc/redis/6379.conf   #(在默认的配置文件路劲中放置配置文件)
    # ln -s /usr/local/redis/utils/redis_init_script /etc/init.d/redisd    #(将初始化文件配置到系统自启动的文件夹内，redisd为服务名，可自行修改)
    # service redisd start   #(开启redis服务，服务名为：redisd)
    # #redis-cli  
    # netstat -ntpl|grep redis
    # echo "Redis 部署完成！"
    # echo " "
    # echo "如果你的系统是Centos 7在安装完毕后留意防火墙，可执行以下命令来放行redis 外部通信。"
    # echo "firewall-cmd --zone=public --add-port=6379/tcp --permanent"    
    # echo "firewall-cmd --reload"
    # echo "firewall-cmd --zone= public --query-port=6379/tcp"
fi # end LINUX


# 判断安装成功没有
if [ ${SUCCESS} == 'true' ]; then
echo_fun 2 安装redis成功
else # [ ${SUCCESS} == 'true' ];
echo_fun 3 安装redis失败
fi # [ ${SUCCESS} == 'true' ];


# 判断服务器配置目录
echo_fun 2 检测数据库目录${REDIS_DATABASE_PATH}
if [ ! -d ${REDIS_DATABASE_PATH} ];then
    echo_fun 2 创建目录${REDIS_DATABASE_PATH}
    sudo mkdir -p ${REDIS_DATABASE_PATH}

    echo_fun 2 拷贝默认配置到${REDIS_MASTER_CONF_PATH}
    sudo cp -pPR /usr/local/etc/redis.conf ${REDIS_MASTER_CONF_PATH}
    
    # sed -i '136s/daemonize no/daemonize yes/' /usr/local/redis/redis.conf    #(编辑redis服务配置文件,修改其中配置)
    # sed -i '69s/127.0.0.1/0.0.0.0/' /usr/local/redis/redis.conf 
    # sed -i '88s/protected-mode yes/protected-mode no/' /usr/local/redis/redis.conf
    sudo chown -R -v ${REDIS_USER}:admin ${REDIS_DATABASE_PATH}

    echo_fun 4 数据库目录初始化完成
fi


echo_fun 2 修改配置文件${REDIS_MASTER_CONF_PATH}
# echo_fun 2 SEDI=====${SEDI}
# 修改监听端口
${SEDI} 's/bind 127.0.0.1 ::1/bind 0.0.0.0 ::/g' ${REDIS_MASTER_CONF_PATH}
${SEDI} "s/port 6379/port ${REDIS_PORT}/g" ${REDIS_MASTER_CONF_PATH}
${SEDI} 's/daemonize no/daemonize yes/g' ${REDIS_MASTER_CONF_PATH}
${SEDI} 's/logfile "\/usr\/local\/var\/log\/redis\/redis.log"/logfile "'${SERVER_REDIS_LOG//\//\\\/}'\/redis.log"/g' ${REDIS_MASTER_CONF_PATH}
${SEDI} 's/logfile ""/logfile "'${SERVER_REDIS_LOG//\//\\\/}'\/redis.log"/g' ${REDIS_MASTER_CONF_PATH}
# 关闭持久化
${SEDI} -e $'/^save 60 10000/a\\\n'"save \"\"
" ${REDIS_MASTER_CONF_PATH}
${SEDI} 's/^save 900 1/#save 900 1/g' ${REDIS_MASTER_CONF_PATH}
${SEDI} 's/^save 300 10/#save 300 10/g' ${REDIS_MASTER_CONF_PATH}
${SEDI} 's/^save 60 10000/#save 60 10000/g' ${REDIS_MASTER_CONF_PATH}
${SEDI} 's/dir \/usr\/local\/var\/db\/redis\//dir .\//g' ${REDIS_MASTER_CONF_PATH}
# 修改密码
${SEDI} 's/# requirepass foobared/requirepass '${REDIS_PASS}'/g' ${REDIS_MASTER_CONF_PATH}
echo_fun 4 修改配置文件${REDIS_MASTER_CONF_PATH}完成
